
layui.define(function(exports){
	var $ = layui.$
	,setter = layui.setter
	,admin = layui.admin
	,carousel = layui.carousel;

	function inietChart() {
		if (sessionStorage.category == 'ORG') {
			$('.LAY_chart_sale').hide();
			return;
		}

		admin.req({
			url: setter.url + '/product/categoryStatistics'
			,type: 'GET'
			,done: function (res) {
				var data = res.data || []
				,categoryNames = data.map(function (item) {
					return item.categoryName;
				})
				,prices = data.map(function (item) {
					return item.price;
				});
	
				var echartsApp = []
				,options = [
					{
						title: {
							text: '产品大类销售额统计',
							x: 'center',
							textStyle: {
								fontSize: 14
							}
						},
						legend: {
							data:['','']
						},
						xAxis : [{
							type : 'category',
							data: categoryNames
						}],
						yAxis : [{
							type : 'value',
							axisLabel:{
								formatter:'{value}'
							}
						}],
						series : [{
							type:'bar',
							name:'销售额',
							data: prices,
							barMaxWidth: 30,
							itemStyle: {        
								normal: {
										label: {
												formatter:'{c}',
												show: true,
												position: 'top',
												textStyle: {
														color: '#333',
														fontSize: 14
												}
										}
								}
							}
						}]
					}
				]
				,view = $('#LAY-sale-dataview').children('div')
				,renderView = function(index){
					echartsApp[index] = echarts.init(view[index], layui.echartsTheme);
					echartsApp[index].setOption(options[index]);
					admin.resize(function(){
						echartsApp[index].resize();
					});
				}
				
				var index = 0;
	
				//没找到DOM，终止执行
				if(view[0]) {
					renderView(0);
				
					//监听数据概览轮播
					carousel.on('change(LAY-sale-dataview)', function(obj){
						renderView(index = obj.index);
					});
				};
	
				//监听侧边伸缩
				layui.admin.on('side', function(){
					setTimeout(function(){
						if(view[0]) {
							renderView(index);
						}
					}, 300);
				});
				
				//监听路由
				layui.admin.on('hash(tab)', function(){
					if(view[0]) {
						layui.router().path.join('') || renderView(index);
					}
				});
			}
		});
	}

	exports('saleChart', {
		init: inietChart
	})
});